#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查数据库中的职位数据
"""

import os
import sys
import django

# Django环境配置
sys.path.append('/Users/baimu/PycharmProjects/2504A/bossxm/boss')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'boss.settings')
django.setup()

from home.models import Jobposting
from user.models import City, Company
from django.db.models import Q

def check_database():
    """检查数据库状态"""
    
    print("🔍 检查数据库中的职位数据...")
    
    try:
        # 统计总数
        total_jobs = Jobposting.objects.count()
        total_cities = City.objects.count()
        total_companies = Company.objects.count()
        
        print(f"📊 数据库统计:")
        print(f"   - 职位总数: {total_jobs}")
        print(f"   - 城市总数: {total_cities}")
        print(f"   - 公司总数: {total_companies}")
        
        if total_jobs > 0:
            print(f"\n🏆 前5个职位样本:")
            jobs = Jobposting.objects.select_related('city', 'company')[:5]
            for i, job in enumerate(jobs, 1):
                city_name = job.city.name if job.city else '未知城市'
                company_name = job.company.name if job.company else '未知公司'
                print(f"   {i}. {job.title} @ {company_name}, {city_name}")
                print(f"      薪资: {job.money}, 描述: {job.description[:50]}...")
        
        # 测试Java相关职位查询
        print(f"\n🔍 测试Java相关职位查询:")
        java_jobs = Jobposting.objects.filter(
            Q(title__icontains='Java') | Q(description__icontains='Java')
        ).select_related('city', 'company')[:3]
        
        print(f"   Java相关职位数量: {java_jobs.count() if hasattr(java_jobs, 'count') else len(list(java_jobs))}")
        for job in java_jobs:
            city_name = job.city.name if job.city else '未知城市'
            company_name = job.company.name if job.company else '未知公司'
            print(f"   - {job.title} @ {company_name}, {city_name}")
        
        # 检查城市数据结构
        print(f"\n🌍 城市数据样本:")
        cities = City.objects.all()[:5]
        for city in cities:
            print(f"   - ID: {city.id}, 城市: {city.name}")
        
        return total_jobs > 0
        
    except Exception as e:
        print(f"❌ 数据库检查失败: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = check_database()
    if success:
        print("\n✅ 数据库检查完成，数据可用")
    else:
        print("\n❌ 数据库检查失败或无数据")
